<template>
    <div class="access_rule_form_container">
        <el-form ref="accessRuleForm" :rules="rules" :model="formData" label-width="100px" v-enterToNext>
            <el-form-item label="编码" prop="code">
                <el-input v-model="formData.code" autocomplete="off"
                          placeholder="请输入编码"></el-input>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="formData.name" autocomplete="off"
                          placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="SQL子句" prop="sql_where">
                <el-input type="textarea" :rows="4" v-model="formData.sql_where"
                          placeholder="请输入请求SQL子句"></el-input>
            </el-form-item>
            <el-form-item label="启用状态" prop="dr">
                <el-radio-group v-model="formData.state">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "AccessForm",
    props: {
        propsData: {
            type: Object,
            default() {
                return {
                    init: true,
                };
            },
        },
    },
    data() {
        return {
            rules: {
                code: [ { required: true, message: '请输入编码', trigger: 'blur' } ],
                name: [ { required: true, message: '请输入名称', trigger: 'blur' } ],
            },
            formData: {},
        };
    },
    watch: {
        propsData: {
            handler() {
                this.init();
            },
            deep: true,
            immediate: true,
        },
    },
    methods: {
        init() {
            const _this = this;
            if (_this.propsData.init) {
                return;
            }
            if (_this.$refs.accessRuleForm) {
                _this.$refs.accessRuleForm.clearValidate();
            }
            _this.formData = $.extend({}, { state: 1, }, _this.propsData.row);
        },
        async confirm() {
            const _this = this;
            let valid = await _this.validateForm('accessRuleForm');
            if (valid) {
                let subData = $.extend({}, _this.formData);
                return _this.fetchData(_this.$api.API_SaveSysDataObjAccessRule, subData).then(() => {
                    _this.$message.success('保存成功！');
                    return true;
                }).catch(() => {
                    return false;
                });
            }
            return valid;
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.access_rule_form_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
